<template>
	<ui-checkbox-group
		v-model="innerValue"
	>
		<ui-checkbox
			v-for="(item, index) in options"
			:key="index"
			:value="item.value"
		>{{item.name}}</ui-checkbox>
	</ui-checkbox-group>
</template>

<script>
	export default {
		name: 'BiDynamicFormControlCheckbox',
		props: {
			/**
			 * [{
			 * 		name: 'xxx',
			 * 		value: 'xxx'
			 *	}] 
			 */
			value: {
				type: Array
			},
			schema: {
				type: Object
			}
		},
		computed: {
			innerValue: {
				get () {
					let value = []
					if (this.value) {
						value = this.value.map(v => v.value)
					}
					return value
				},
				set (val) {
					if (val && val.length) {
						val = val.map(v => {
							const opt = this.options.find(vv => vv.value === v)
							if (opt) {
								return {
									name: opt.name,
									value: opt.value
								}
							}
						}).filter(v => !!v)
						this.$emit('input', val)
					} else {
						this.$emit('input', val)
					}
				}
			},
			options () {
				return this.schema.items || []
			}
		}
	}
</script>

<style>
</style>
